package com.service.impl;

import com.constant.MqttMessageType;
import com.dao.*;
import com.emqx.MqttMessageDTO;
import com.emqx.MqttMessageSender;
import com.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.*;
import com.page.GenericPage;
import com.po.DeviceRelationAppVersionSaveOrUpdatePO;
import com.po.PlacedHospitalRelationProductQueryPO;
import com.po.PlacedHospitalRelationProductSaveOrUpdatePO;
import com.result.Result;
import com.service.DeviceRelationAppVersionService;
import com.service.PlacedHospitalRelationProductService;
import com.vo.PlacedHospitalRelationProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DeviceRelationAppVersionServiceImpl implements DeviceRelationAppVersionService {

    @Resource
    DeviceRelationAppVersionMapper deviceRelationAppVersionMapper;


    @Resource
    VenderDeviceMapper venderDeviceMapper;

    @Resource
    AppVersionUpgradeMapper appVersionUpgradeMapper;



    @Autowired
    private MqttMessageSender mqttMessageSender;

    @Override
    @Transactional
    public Result<Boolean> saveOrUpdate(DeviceRelationAppVersionSaveOrUpdatePO record) {
        if (ObjectUtils.isEmpty(record.getDeivceId())||ObjectUtils.isEmpty(record.getAppVersionId())  ){
            throw  new BusinessException("参数异常");
        }
        DeviceRelationAppVersion deviceRelationAppVersion = deviceRelationAppVersionMapper.selectByDeviceId(record.getDeivceId());



        if (ObjectUtils.isEmpty(deviceRelationAppVersion)){
           //添加
            deviceRelationAppVersion = new DeviceRelationAppVersion();
            deviceRelationAppVersion.setDeviceId(record.getDeivceId());
            deviceRelationAppVersion.setAppVersionId(record.getAppVersionId());
            deviceRelationAppVersionMapper.insert(deviceRelationAppVersion);
        }else{
            if (record.getAppVersionId()<deviceRelationAppVersion.getAppVersionId()){
                throw  new BusinessException("关联的应用版本过低，请重新选择");
            }
            deviceRelationAppVersion.setAppVersionId(record.getAppVersionId());
            deviceRelationAppVersionMapper.updateByPrimaryKeySelective(deviceRelationAppVersion);
        }

        AppVersionUpgrade appVersionUpgrade = appVersionUpgradeMapper.selectByPrimaryKey(record.getAppVersionId());
        if (appVersionUpgrade.getReleastStatus()!=2){
            appVersionUpgrade.setReleastStatus(2);
            appVersionUpgradeMapper.updateByPrimaryKeySelective(appVersionUpgrade);
        }
        VenderDevice device  =  venderDeviceMapper.selectByPrimaryKey(record.getDeivceId());
        device.setBindAppVersion(appVersionUpgrade.getApkName());
        venderDeviceMapper.updateByPrimaryKeySelective(device);

        //发送通知
        Date now = new Date();
        MqttMessageDTO message=new MqttMessageDTO();
        message.setMsgSender("server_in");
        message.setMsgReceiver(device.getDeviceSn());
        message.setMsgType(MqttMessageType.CHECK_APK_VERSION);
        message.setMsgBody("");
        message.setSendTime(now);
        mqttMessageSender.send(message);
        return new Result<>().ok();
    }

//    @Override
//    public Result<Boolean> modifyById(PlacedHospitalRelationProduct record) {
//        if (ObjectUtils.isEmpty(record.getId()) ||
//                ObjectUtils.isEmpty(record.getFactPrice()) || record.getFactPrice().doubleValue() < 0.01 ){
//            throw  new BusinessException("参数异常");
//        }
//        record.setHospitalId(null);
//        record.setProductId(null);
//        placedHospitalRelationProductMapper.updateByPrimaryKeySelective(record);
//        return new Result<>().ok();
//    }
//
//    @Override
//    public Result<PlacedHospitalRelationProductVO> queryById(Long id) {
//        PlacedHospitalRelationProduct relation = placedHospitalRelationProductMapper.selectByPrimaryKey(id);
//
//        if (!ObjectUtils.isEmpty(relation)){
//            Product product = productMapper.selectByPrimaryKey(relation.getProductId());
//            PlacedHospitalRelationProductVO placedHospitalRelationProductVO = new PlacedHospitalRelationProductVO();
//            BeanUtils.copyProperties(product,placedHospitalRelationProductVO);
//            placedHospitalRelationProductVO.setFactPrice(relation.getFactPrice());
//            placedHospitalRelationProductVO.setHospitalId(relation.getHospitalId());
//            return new Result<PlacedHospitalRelationProductVO>().ok(placedHospitalRelationProductVO);
//
//        }
//        return new Result<PlacedHospitalRelationProduct>().ok();
//    }
//
//    @Override
//    public Result<GenericPage<PlacedHospitalRelationProductVO>> queryAll(PlacedHospitalRelationProductQueryPO record) {
//        if (ObjectUtils.isEmpty(record.getHospitalId())){
//            throw new BusinessException("参数异常");
//        }
//
//        int pageNo = ObjectUtils.isEmpty(record.getPageNo())|| record.getPageNo()==0?1:record.getPageNo();
//        int pageSize = ObjectUtils.isEmpty(record.getPageSize())|| record.getPageSize()==0?10:record.getPageSize();
//
//        PageHelper.startPage(pageNo, pageSize);
//        List<Product> products = productMapper.selectByCondition(new Product());
//
//        List<PlacedHospitalRelationProductVO> list = new ArrayList<>();
//        PlacedHospitalRelationProductVO placedHospitalRelationProductVO = null;
//        for (Product product:products){
//            placedHospitalRelationProductVO = new PlacedHospitalRelationProductVO();
//            BeanUtils.copyProperties(product,placedHospitalRelationProductVO);
//            PlacedHospitalRelationProduct relation = placedHospitalRelationProductMapper.selectByHospitalIdAndProductId(record.getHospitalId(), product.getId());
//            if (!ObjectUtils.isEmpty(relation)){
//                placedHospitalRelationProductVO.setFactPrice(relation.getFactPrice());
//            }else{
//                if (!ObjectUtils.isEmpty(product.getProductPrice())){
//                    //售卖商品
//                    placedHospitalRelationProductVO.setFactPrice(product.getProductPrice());
//                }else if (!ObjectUtils.isEmpty(product.getRentPricePerDay()) || !ObjectUtils.isEmpty(product.getRentPricePerHour())){
//                    //租赁商品
//                    if (!ObjectUtils.isEmpty(product.getRentPricePerDay())){
//                        //日租
//                        placedHospitalRelationProductVO.setFactPrice(product.getRentPricePerDay());
//                    }else {
//                        //时租
//                        placedHospitalRelationProductVO.setFactPrice(product.getRentPricePerHour());
//                    }
//                }
//
//            }
//            placedHospitalRelationProductVO.setHospitalId(record.getHospitalId());
////            placedHospitalRelationProductVO.setAgent(record.getAgent());
//            list.add(placedHospitalRelationProductVO);
//        }
//        PageInfo<PlacedHospitalRelationProductVO> pageInfo = new PageInfo<>(list);
//        GenericPage<PlacedHospitalRelationProductVO> genericPage = new GenericPage<PlacedHospitalRelationProductVO>(pageSize, pageInfo.getTotal(), list);
//        return new Result<GenericPage<PlacedHospitalRelationProductVO>>().ok(genericPage);
//    }
}
